package com.ivanceras.server;

import java.util.ArrayList;
import java.util.List;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
import com.google.appengine.api.datastore.Key;


@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Questionnaire{
	@PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    public Long questionnaireID;
    
    @Persistent
	public String title;

    @Persistent
    private int items;
    
    @Persistent
    public String description;
    
    public List<Question> questions  = new ArrayList<Question>();
    
    public Questionnaire(){
    	items = 0;
    }
    public Questionnaire(String title){
    	this.title = title;
    }
    public Questionnaire(String title, String description){
    	this(title);
    	this.description = description;
    }
    /**
     * 
     * @param questionText
     * @param choices
     */
    /*
    public Question addQuestion(String questionText, ArrayList<Choice> choices){
    	Question q = new Question();
    	q.setText(questionText);
    	for(int i = 0; i < choices.size(); i++){
    		Choice qc = choices.get(i);
    		q.addChoice(qc.text, qc.isCorrect);
    	}
    	questions.add(q);
    	items = questions.size();
    	System.out.println("question is added with ID: "+q.key.getId());
    	return q;
    }
    */
	public void addQuestion(Question question){
		questions.add(question);
    	items = questions.size();
	}
    public List<Question> getQuestions(){
    	return questions;
    }
    /**
     * a cheap way to determine the number of items than retrieving the actual questions
     * @return
     */
    public int getItems(){
    	System.out.println("items: "+items);
    	return items;
    }
    public void setTitle(String title){
    	this.title = title;
    }
    public void setDescription(String description){
    	this.description = description;
    }
    public void saveQuestion(Question oldQuestion, Question newQuestion){
    	int index = this.questions.indexOf(oldQuestion);
    	System.out.println("This question is in: "+index);
    	Question question = questions.get(index);
    	question.setText(newQuestion.text);
    	question.setChoices(newQuestion.getChoices());
    	System.out.println("saved!"+questions.get(index).text);
    	System.out.println("from!"+question.text);
    }
}
